﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kiosk.Library;
using System.ComponentModel;
using System.Threading;

namespace Kiosk.UserControls
{
    /// <summary>
    /// Interaction logic for NotificationPresenter.xaml
    /// </summary>
    public partial class NotificationPresenter : UserControl
    {
        public event NotificationClosedEventHandler Closed;
        BackgroundWorker bw;
        public NotificationPresenter(NotificationInfo ni)
        {
            InitializeComponent();
            this.DataContext = ni;
            bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(SendAlert_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            map.EndPoint = ni.LocationPoint;
            map.EndLocationName = ni.LocationName;
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bool success = (bool)e.Result;
            if (!success)
            {
                MessageBox.Show("Error sending alert, please check your mail settings");
            }
        }

        void SendAlert_DoWork(object sender, DoWorkEventArgs e)
        {
            bool success = true;
            AlertArgs aArgs = (AlertArgs)e.Argument;
            try
            {
                EmployeeInfo emp = EmployeeInfo.GetEmployeeInfoById(aArgs.EmpID);
                string email = "",
                    subject = "",
                    body = "";
                if (aArgs.AlertType == "Email")
                {
                    subject = "Kiosk Alert: " + aArgs.Title;
                    body = "You have received an alert from the kiosk regarding this notification.";
                    email = emp.Email;
                }
                else if (aArgs.AlertType == "Text")
                {
                    subject = "Kiosk Alert";
                    body = "You have received an alert from the kiosk regarding your notification: " + aArgs.Title;
                    email = emp.PhoneEmail;
                }
                Kiosk.Utilities.Email.Send(emp.Email, subject, body);
            }
            catch (Exception ex)
            {
                success = false;
            }

            e.Result = success;

        }



        private void btnEmail_MouseUp(object sender, MouseButtonEventArgs e)
        {
            while (bw.IsBusy)
            { Thread.Sleep(10); }
            NotificationInfo n = (NotificationInfo)this.DataContext;
            bw.RunWorkerAsync( new AlertArgs(){AlertType = "Email", EmpID = n.EmployeeID, Title = n.Title });
        }

        private void btnText_MouseUp(object sender, MouseButtonEventArgs e)
        {
            while (bw.IsBusy)
            { Thread.Sleep(10);  }
            NotificationInfo n = (NotificationInfo)this.DataContext;
            bw.RunWorkerAsync(new AlertArgs() { AlertType = "Text", EmpID = n.EmployeeID, Title = n.Title });

        }

        private void btnCall_MouseUp(object sender, MouseButtonEventArgs e)
        {
        }

        private void btnClose_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Closed(this, new EventArgs());
        }
    }

    public struct AlertArgs
    {
        public string AlertType;
        public Guid EmpID;
        public string Title;
    }

    public delegate void NotificationClosedEventHandler(object sender, EventArgs e);

}
